using System;
using System.Data;
using System.Threading;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
using Avalonia.VisualTree;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace SocketTool.Controls
{
    public partial class GrowlControl : UserControl
    {
        public enum GrowType
        {
            Success,
            Error,
            Warning,
            Info
        }

        private Button _closeButton;

        private static StackPanel mParentStack;

        /// <summary>
        /// 设置父容器
        /// </summary>
        /// <param name="parent"></param>
        public static void SetParentStack(StackPanel parent)
        {
            mParentStack = parent;
        }

        /// <summary>
        /// 移除提示
        /// </summary>
        /// <param name="growl"></param>
        public static void RemoveGrowl(GrowlControl growl)
        {
            mParentStack.Children.Remove(growl);
        }

        /// <summary>
        /// Success Tips
        /// </summary>
        /// <param name="content"></param>
        public static void Success(string content)
        {
            ShowGrowControl(content, GrowType.Success);
        }

        /// <summary>
        /// Error Tips
        /// </summary>
        /// <param name="content"></param>
        public static void Error(string content)
        {
            ShowGrowControl(content, GrowType.Error);
        }

        /// <summary>
        /// Info Tips
        /// </summary>
        /// <param name="content"></param>
        public static void Info(string content)
        {
            ShowGrowControl(content, GrowType.Info);
        }

        /// <summary>
        /// Warning Tips
        /// </summary>
        /// <param name="content"></param>
        public static void Warning(string content)
        {
            ShowGrowControl(content,GrowType.Warning);
        }

        /// <summary>
        /// 弹框提示
        /// </summary>
        /// <param name="content"></param>
        private static void ShowGrowControl(string content, GrowType type)
        {
            var growl = new GrowlControl(content, type);
            mParentStack.Children.Insert(0, growl);
        }

        #region 非静态方法

        private System.Threading.Timer _aotuCloseTimer = null;

        public GrowlControl()
        {
            InitializeComponent();
        }

        public GrowlControl(string content, GrowType type)
        {
            InitializeComponent();

            var growlTips = this.FindControl<TextBlock>("TxtTips");
            var growlTime = this.FindControl<TextBlock>("TxtDateTime");
            var img = this.FindControl<Image>("ImageIcon");

            growlTips.Text = content;
            growlTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            
            Uri  uri = null;
            if (type == GrowType.Error)
                uri = new Uri("avares://SocketTool/Resources/Images/Error.png");
            else if (type == GrowType.Info)
                uri = new Uri("avares://SocketTool/Resources/Images/InfoIcon.png");
            else if (type == GrowType.Success)
                uri = new Uri("avares://SocketTool/Resources/Images/SuccessIcon.png");
            else if (type == GrowType.Warning)
                uri = new Uri("avares://SocketTool/Resources/Images/WarningIcon.png");
            var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
            var asset = assets.Open(uri);
            img.Source = new Bitmap(asset);
            this._aotuCloseTimer =
                new Timer(_ =>
                {
                    Dispatcher.UIThread.InvokeAsync(() =>
                    {
                        this.CloseButton_OnClick(null, null);
                    });
                }, null, 5000, Timeout.Infinite);
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
            _closeButton = this.FindControl<Button>("CloseButton");
            this.PointerEnter += OnPointerEnter;
            this.PointerLeave += OnPointerLeave;
        }

        private void OnPointerLeave(object? sender, PointerEventArgs e)
        {
            _closeButton.IsVisible = false;
        }

        private void OnPointerEnter(object? sender, PointerEventArgs e)
        {
            _closeButton.IsVisible = true;
        }

        /// <summary>
        /// Close
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void CloseButton_OnClick(object? sender, RoutedEventArgs e)
        {
            GrowlControl.RemoveGrowl(this);
        }

        #endregion
    }
}